# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request, Selector, linkextractors
from scrapy.http import Response, HtmlResponse
from scrapy.linkextractors.htmlparser import HtmlParserLinkExtractor
from scrapy.spiders import Rule

from XiaoHua.items import XiaohuaItem


class GirlsSpider(scrapy.Spider):
    name = 'girls'
    allowed_domains = ['xiaohuar.com']

    def parse_girls_info(self, response):
        assert isinstance(response, Response)
        girls_div = response.xpath("//*[@id=\"list_img\"]/div/div[1]")[0]
        assert isinstance(girls_div, Selector)
        for girl_div in girls_div.xpath("div"):
            img_tag = girl_div.xpath("div[1]/div[1]/a/img")
            item = XiaohuaItem()
            img_src = img_tag.attrib["src"]
            img_name = img_tag.attrib['alt']
            item['name'] = img_name
            item['image'] = img_src
            item["image_urls"] =["http://www.xiaohuar.com" + img_src]
            yield item

        all_pages_div = response.xpath("//*[@id=\"page\"]/div")
        for link in all_pages_div.xpath("a"):
            info = link.xpath("text()").extract()[0]
            if info == "下一页":
                yield Request(link.attrib['href'], callback=self.parse_girls_info)

    def start_requests(self):
        yield Request("http://www.xiaohuar.com/list-1-1.html",
                      callback=self.parse_girls_info,
                      method="GET")

#
# class GirlsSpider(scrapy.spiders.CrawlSpider):
#     name = 'girls'
#     rules = (Rule(linkextractors.LinkExtractor(), callback="parse_item"),)
#     start_urls = ["http://www.xiaohuar.com/list-1-1.html"]
#
#     def parse_item(self, response):
#         assert isinstance(response, Response)
#         girls_div = response.xpath("//*[@id=\"list_img\"]/div/div[1]")[0]
#         assert isinstance(girls_div, Selector)
#         for girl_div in girls_div.xpath("div"):
#             img_tag = girl_div.xpath("div[1]/div[1]/a/img")
#             item = XiaohuaItem()
#             img_src = img_tag.attrib["src"]
#             img_name = img_tag.attrib['alt']
#             item['name'] = img_name
#             item['image'] = img_src
#             # item["image_urls"] = ["http://www.xiaohuar.com" + img_src]
#             yield item
#
#     def parse_start_url(self, response):
#         assert isinstance(response, HtmlResponse)
#         # response.replace("body", response.body.decode('gb18030'))
#         # response.body =
#         return []
#         # all_pages_div = response.xpath("//*[@id=\"page\"]/div")
#         # for link in all_pages_div.xpath("a"):
#         #     info = link.xpath("text()").extract()[0]
#         #     if info == "下一页":
#         #         yield Request(link.attrib['href'], callback=self.parse_girls_info)
#     # def parse_start_urls(self, response):
#
#
#
